package com.goldplusgold.tradedelay.app.home.live.adapter;

import android.content.Context;
import android.content.Intent;
import android.database.DataSetObserver;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.goldplusgold.imageloader.lib.IShow;
import com.goldplusgold.live.LVDataManager;
import com.goldplusgold.live.bean.ColumnBean;
import com.goldplusgold.live.result.ResultColumns;
import com.goldplusgold.support.lib.base.adapter.BaseListAdapter;
import com.goldplusgold.support.lib.utils.ScreenSizeUtil;
import com.goldplusgold.support.lib.widget.pulltorefresh.PullToRefreshListView;
import com.goldplusgold.tradedelay.app.R;
import com.goldplusgold.tradedelay.app.home.live.ColumnInfoActivity;
import com.goldplusgold.tradedelay.app.home.live.LiveDetailActivity;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by Administrator on 2017/7/10.
 */

public class LiveListAdapter extends BaseListAdapter<List<ColumnBean>> {

    private List<ColumnBean> datas = new ArrayList<>();
    PullToRefreshListView listView;
    public LiveListAdapter(){

    }

    @Override
    public void setPullView(PullToRefreshListView listView){
        this.listView = listView;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        return false;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public int getCount() {
        if(datas != null){
            return datas.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(datas != null){
            return datas.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.live_item_layout, null);
            holder = new ViewHolder();
            holder.middle_live_layout = (RelativeLayout)convertView.findViewById(R.id.middle_live_layout);
            holder.middle_live_layout.setVisibility(View.VISIBLE);
            holder.contentImg = (ImageView) convertView.findViewById(R.id.live_item_layout_content_img);
            holder.name = (TextView) convertView.findViewById(R.id.live_item_layout_txt_name);
            holder.description = (TextView) convertView.findViewById(R.id.live_item_layout_txt_description);
            holder.icon = (ImageView) convertView.findViewById(R.id.live_item_layout_img_icon);
            holder.detailImg = (ImageView) convertView.findViewById(R.id.live_item_layout_detail_img);
            holder.contentStateImg = (TextView)convertView.findViewById(R.id.live_item_layout_content_state_img);
            holder.contentTitle = (TextView) convertView.findViewById(R.id.live_item_layout_content_title);
            holder.contentState = (TextView) convertView.findViewById(R.id.live_item_layout_content_state);
            holder.controlImg = (TextView) convertView.findViewById(R.id.live_item_control_img);
            holder.txtState = (TextView)convertView.findViewById(R.id.live_item_txt_state) ;
            holder.middle_view = (RelativeLayout)convertView.findViewById(R.id.middle_view);
            holder.state_ly = (LinearLayout) convertView.findViewById(R.id.state_ly);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        bindData(holder, position);
        return convertView;
    }


    //TODO 代码优化 by hur
    private void bindData(ViewHolder holder, final int position){
        refresh = true;
        holder.name.setText(datas.get(position).getColumnName());
        IShow.getInstance().showCircle(context,datas.get(position).getColumnIcon(),holder.icon);

        IShow.getInstance().showRound(context , datas.get(position).getLiveIcon() , holder.contentImg
        , R.drawable.live, ScreenSizeUtil.dip2px(context , 132),ScreenSizeUtil.dip2px(context , 75));

        holder.description.setText(datas.get(position).getOneWordIntro());
        if(!TextUtils.isEmpty(datas.get(position).getLivePrevue())) {
            if(TextUtils.isEmpty(datas.get(position).getLivePrevueTime())){
                holder.txtState.setText(context.getText(R.string.livePrevue) + datas.get(position).getLivePrevue());
            }else{
                holder.txtState.setText( context.getText(R.string.livePrevue) + datas.get(position).getLivePrevue() + " "
                        +datas.get(position).getLivePrevueTime() );
            }
        }else{
            holder.txtState.setText(context.getText(R.string.livePrevue) + " 暂无");
        }
        holder.contentTitle.setText(datas.get(position).getLiveName());
        if( datas.get(position).getLiveType() == 0){
            holder.contentState.setText(R.string.live_wtext);
        } else{
            holder.contentState.setText(R.string.live_vtext);
        }
        if(datas.get(position).getLiveStatus() == 0 || datas.get(position).getLiveStatus() == 20){
            holder.contentStateImg.setVisibility(View.GONE);

            holder.contentState.setText(datas.get(position).getLiveTime() + " 即将开播");
            holder.contentState.setTextColor(context.getResources().getColor(R.color.color_gold));
            holder.state_ly.setBackground(context.getResources().getDrawable(R.drawable.shape_follow_select));
        }
        if(datas.get(position).getLiveStatus() == 10 ||  datas.get(position).getLiveStatus() == 15 ){
            holder.contentStateImg.setVisibility(View.VISIBLE);
            holder.contentStateImg.setBackgroundResource(R.drawable.live_icon_end);
            holder.contentState.setText("直播结束");
            holder.contentState.setTextColor(context.getResources().getColor(R.color.follower_text) );
            holder.state_ly.setBackground(context.getResources().getDrawable(R.drawable.shape_follow_normal));
        }
        if(datas.get(position).getLiveStatus() == 30){
            holder.contentStateImg.setVisibility(View.VISIBLE);
            holder.contentStateImg.setBackgroundResource(R.drawable.home_icon_middle);
            if( datas.get(position).getLiveType() == 0){
                holder.contentState.setText(R.string.live_wtext);
            } else{
                holder.contentState.setText(R.string.live_vtext);
            }
            holder.contentState.setTextColor(context.getResources().getColor(R.color.color_gold));
            holder.state_ly.setBackground(context.getResources().getDrawable(R.drawable.shape_follow_select));
        }
        holder.middle_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,LiveDetailActivity.class);
                intent.putExtra("liveID",datas.get(position).getLiveID());
                intent.putExtra("roomID",datas.get(position).getRoomID());
                if(datas.get(position).getLiveType() == 0){
                    intent.putExtra("type", LiveDetailActivity.LiveDetailType.TEXT_LIVE);
                }else{
                    intent.putExtra("type", LiveDetailActivity.LiveDetailType.LIVE);
                }

                context.startActivity(intent);
            }
        });
        holder.middle_live_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //todo 修改了跳转页面
                Intent intent = new Intent(context, /*ColumnIntroActivity.class*/ColumnInfoActivity.class);
                intent.putExtra("columnID",datas.get(position).getColumnID());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public void upRereshView() {
        requestData();
    }

    @Override
    public void downRereshView() {
        this.datas.clear();
        requestData();
    }

    @Override
    public void setDatas(final List<ColumnBean> list) {
        if(list != null){
            datas.addAll(list);
        }
        if(datas.size() == 0){
            return;
        } else{
            send();
        }
    }

    @Override
    public void notifyChange(){
        if(listView != null){
            listView.onRefreshComplete();
        }
        listView.getRefreshableView().setAdapter(this);
        notifyDataSetChanged();
    }

    @Override
    public void setContext(Context context) {
        super.setContext(context);
    }

    @Override
    public void requestData(){
        refresh = false;
        LVDataManager.getInstance().getColumns(new Callback<ResultColumns>() {
            @Override
            public void onResponse(Call<ResultColumns> call, Response<ResultColumns> response) {
                    if(response.body() != null){
                        setDatas(response.body().getColumns());
                    } else{
                        send();
                    }
            }

            @Override
            public void onFailure(Call<ResultColumns> call, Throwable t) {
                    send();
            }
        });
    }

    public static class ViewHolder {
        public TextView name;
        public ImageView icon;
        public TextView description;
        public ImageView detailImg;
        public ImageView contentImg;
        public TextView contentStateImg;
        public TextView contentTitle;
        public TextView contentState;
        public TextView controlImg;
        public TextView txtState;
        RelativeLayout middle_live_layout;
        RelativeLayout middle_view;
        LinearLayout state_ly ;
    }
}
